﻿/*
 * Copyright 2008 Lucifer Lu
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Windows;
using System.Windows.Media;
using SilverChart.Util;
using SilverChart.Configuration;

namespace SilverChart.Components {
    public partial class LegendComponent : CompositeChartWidget {
        private ChartBase Chart;

        public LegendComponent()
            : this(null) {
        }

        public LegendComponent(LegendProperties property) {
            InitializeComponent();
            Chart = property.ParentChart;
            if (Chart != null) {
                LegendText.Text = property.LegendName;
                LegendRect.Fill = property.LegendFill;
                LegendRect.Stroke = property.LegendStroke;

                LegendRect.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(LegendRect_MouseLeftButtonUp);
                LegendRect.MouseEnter += new System.Windows.Input.MouseEventHandler(LegendRect_MouseEnter);
                LegendRect.MouseLeave += new System.Windows.Input.MouseEventHandler(LegendRect_MouseLeave);
            }
            else {
                LegendText.Text = "PlaceHolder";
                LegendRect.Fill = BrushHelper.GetSolidColorBrush("Yellow");
                LegendRect.Stroke = BrushHelper.GetSolidColorBrush("Black");
            }
        }

        void LegendRect_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e) {
            Chart.NormalizeChart();
        }

        void LegendRect_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e) {
            Chart.HighLightChart();
        }

        void LegendRect_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) {
            Chart.BringChartToFront();
        }

        public override string WidgetName {
            get {
                return "Legend Component";
            }
        }

        protected override Size MeasureOverride(Size availableSize) {
            LegendRect.Measure(availableSize);
            LegendText.Measure(availableSize);
            Size legendSize = new Size(LegendRect.DesiredSize.Width + LegendText.DesiredSize.Width,
                Math.Max(LegendRect.DesiredSize.Height, LegendText.DesiredSize.Height));
            return legendSize;
        }

        protected override Size ArrangeOverride(Size finalSize) {
            Rect rectRect = new Rect(0, 0, LegendRect.DesiredSize.Width, finalSize.Height);
            LegendRect.Arrange(rectRect);
            Rect textRect = new Rect(LegendRect.DesiredSize.Width, 0,
                finalSize.Width - LegendRect.DesiredSize.Width, finalSize.Height);
            LegendText.Arrange(textRect);
            return finalSize;
        }
    }
}
